<?php
namespace app\index\controller;

use think\facade\Request;
use think\facade\Session;
use think\facade\Cookie;
use app\common\service\AdminUser as S;
use app\common\service\AdminUserscore as US;
use think\facade\View;
use think\facade\Cache;
class Cdm extends \app\BaseController
{
    protected $middleware = ['WxCheck'];
    public function index()
    {
        // if(S::isLogin())
        // {
        //     $info=Session::get('xfuser');
        //     $token=Cookie::get('xftoken');
        //     //echo($token);
        // }
        // else{
        //     $rurl= urlencode(request()->url());
        //     //echo $rurl;
        //     header("Location:/wx/wxlogin?rurl=".$rurl);
        // }
       // dump(get_curuser()["nickname"]);
       header('X-Frame-Options:ALLOW-FROM http://xf.xingtaiwang.net');
       
       
       return $this->fetch('index',['cuser'=>get_curuser()]);
    }
    //保存分数
    public function savescore()
    {
        
        $check = Request::checkToken('__token__');
        if(false === $check) {
            return $this->getJson(['msg' => '验证失败', 'code' => 204]);
        }
        //openid 从sesssion中获取
        $openid=get_curuser()["openid"];
        if (Request::isAjax()) {
            
            $user_agente = $_SERVER['HTTP_USER_AGENT'];
            //判断微信
            if (strpos($user_agente, 'MicroMessenger') == false) {die; }
            //判断是否当前域名下ntp.diandianx.com
            if(!isset($_SERVER['HTTP_REFERER']) || !stripos($_SERVER['HTTP_REFERER'],'xf.xingtaiwang.net')) {die;}
            if(!Request::isPost()){die;}
            $time1=date("Y-m-d H:i:s");                              
            $time2="2022-02-16 10:00:00";  
            $can=betweenTime($time1,$time2);
            if($can){
                $postdata=['xmid'=>1,"openid"=>$openid, 'ip'=>Request::ip(), 'score'=>Request::param('score')];
                return $this->getJson(US::saveScore($postdata));
            }else{
                return $this->getJson(['msg' => '活动已结束，感谢参与！', 'code' => 201]);
            }
        }
    }
    //排行
    public function rank()
    {
        if (Request::isAjax()) {
           return $this->getJson(US::getindexlist());
        }
        else{
            return $this->fetch('rank',['cuser'=>get_curuser()]);
        }
    }
      //我的
    public function my()
    {
        return $this->fetch();
    }
    public function address()
    {
        if (Request::isAjax()) {
            $id=get_curuser()['id'];
            return $this->getJson(S::address(Request::post(),$id));
        }
    }
    //扫码信息
    public function getscore()
    {
       if (Request::isAjax()) {
           return US::goStatus(Request::param('status'),Request::param('id'));
       }
        $toplist=Cache::get('toplist');
        if($toplist)
        {
            return $this->fetch('getscore',['cuser'=>$toplist]);
        }
        else{
           //从前200名中查找
            $limit=200;
            $list=US::getlist();
           // dump($list->openid);
            $mingci=1;
            $cuser=get_curuser();
            $hasaddres= S::hasaddress($cuser['id']);
            $openid=$cuser['openid'];
            $cuser["rank"]=0;
            $cuser["score"]=0;
            $cuser["id"]=0;
            $cuser['hasaddress']=$hasaddres;
            foreach($list as $item)
            {  
               // echo($item);
                if($item->openid==$openid)
                {
                    $cuser["rank"]=$mingci;
                    $cuser["score"]=$item['score'];
                    $cuser["id"]=$item['id'];
                    Cache::set('toplist', $cuser, 600);
                    break;
                }
                $mingci++;
            }
          
            return $this->fetch('getscore',['cuser'=>$cuser]);
        }
    }
}
